<?php

function get_product_detail_by_name($name) {

    include(dirname(__FILE__) . '/../config.php');
    include_once(dirname(__FILE__) . '/../util.php');
    $con = mysqli_connect($host, $user, $pass, $db);
    mysqli_set_charset($con, "utf8");
// Check connection
    if (mysqli_connect_errno()) {
        die("Failed to connect to MySQL: " . mysqli_connect_error());
    }
    $name = mysql_real_escape_string($name);
    $sql = "SELECT * FROM products where name='" . $name . "'";

    mysqli_query($con, "SET NAMES 'utf8'");
    $result = mysqli_query($con, $sql);
    if (!$result) {
        die(mysqli_error($con) . ':' . $sql);
    }
    
    $contact = array();
    while ($row = mysqli_fetch_array($result)) {
        $contact = $row;
    }
    return $contact;
    mysqli_close($con);
}

function get_product_detail($id) {
    include(dirname(__FILE__) . '/../config.php');
    include_once(dirname(__FILE__) . '/../util.php');
    $con = mysqli_connect($host, $user, $pass, $db);
    mysqli_set_charset($con, "utf8");
// Check connection
    if (mysqli_connect_errno()) {
        die("Failed to connect to MySQL: " . mysqli_connect_error());
    }
    $id = mysql_real_escape_string($id);
    $sql = "SELECT * FROM products where id=" . $id;

    mysqli_query($con, "SET NAMES 'utf8'");
    $result = mysqli_query($con, $sql);
    if (!$result) {
        die(mysqli_error($con) . ':' . $sql);
    }
    $is_login = 0;
    $contact = array();
    while ($row = mysqli_fetch_array($result)) {
        $contact = $row;
    }
    return $contact;
    mysqli_close($con);
}



function get_all_products($ids = null) {
    require('../config.php');
    $con = mysqli_connect($host, $user, $pass, $db);
    mysqli_set_charset($con, "utf8");
// Check connection
    if (mysqli_connect_errno()) {
        die("Failed to connect to MySQL: " . mysqli_connect_error());
    }

    $sql = "SELECT * FROM products where 1";
    $append_str = '';
    if ($ids != null && is_array($ids)) {

        $append_str.=implode(",", $ids);
        $sql.=' AND id in (' . $append_str . ')';
    }

    mysqli_query($con, "SET NAMES 'utf8'");
    $result = mysqli_query($con, $sql);
    if (!$result) {
        die(mysqli_error($con) . ':' . $sql);
    }
    
    $contacts = array();
    while ($row = mysqli_fetch_array($result)) {
        $contacts[] = $row;
    }
    mysqli_close($con);
    return $contacts;
}

function add_product($post) {
    require('../config.php');
    //require('../util.php');
    $con = mysqli_connect($host, $user, $pass, $db);

// Check connection
    if (mysqli_connect_errno()) {
        die("Failed to connect to MySQL: " . mysqli_connect_error());
    }
    $keys = array();
    $values = array();
    foreach ($post as $column_name => $value) {
        if ($column_name != 'go') {
            $value = trim($value);
            $value = mysql_real_escape_string($value);
            $keys[] = "`{$column_name}`";
           
            $values[] = "'{$value}'";
        }
    }
    $query = "INSERT INTO products (" . implode(",", $keys) . ") VALUES (" . implode(",", $values) . ");";
    
    mysqli_query($con, "SET NAMES 'utf8'");
    $result = mysqli_query($con, $query);
    if (!$result) {
        die(mysqli_error($con) . ':' . $query);
    }
    
    mysqli_close($con);
}


function update_product($post) {
    include(dirname(__FILE__) . '/../config.php');
    include_once(dirname(__FILE__) . '/../util.php');
    $con = mysqli_connect($host, $user, $pass, $db);

// Check connection
    if (mysqli_connect_errno()) {
        die("Failed to connect to MySQL: " . mysqli_connect_error());
    }
    $keys = array();
    $values = array();
    $query = 'Update products SET ';
    foreach ($post as $column_name => $value) {
        if ($column_name != 'go') {
            $value = trim($value);
            $value = mysql_real_escape_string($value);
           
                $query.=" `{$column_name}` = " . " '{$value}',";
            
        }
    }
    $query.="`id`=" . $post['id'];
    $query.=" Where id =" . $post['id'];


    mysqli_query($con, "SET NAMES 'utf8'");
    
    $result = mysqli_query($con, $query);
    if (!$result) {
        die(mysqli_error($con) . ':' . $query);
    }
    //echo 'query='.$query;
    //die();
    mysqli_close($con);
}



function delete_product($id) {
    require('../config.php');

    $con = mysqli_connect($host, $user, $pass, $db);

    // Check connection
    if (mysqli_connect_errno()) {
        die("Failed to connect to MySQL: " . mysqli_connect_error());
    }

    $id = mysql_real_escape_string($id);
    $query = 'DELETE FROM product WHERE id = ' . $id;
    mysqli_query($con, "SET NAMES 'utf8'");
    mysqli_query($con, $query);
    mysqli_close($con);
}



?>